package deliciouse.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import deliciouse.biz.MessagesBiz;
import deliciouse.biz.impl.MessagesBizImpl;
import deliciouse.entity.Messages;

@WebServlet("/Message")
public class MessageListServlet extends HttpServlet{

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int pageNum = 1;
		if (request.getParameter("pageNum") != null && !request.getParameter("pageNum").isEmpty())
			pageNum = Integer.valueOf(request.getParameter("pageNum"));
		int pageSize = 3;
		int pageNumMax = 0;
		int total = 0;
		MessagesBiz messagesbiz = new MessagesBizImpl();
		pageNumMax = messagesbiz.MessageMaxRows();
		total = pageNumMax % pageSize == 0 ? (pageNumMax / pageSize) : (pageNumMax / pageSize + 1);
		List<Messages> messageslist = messagesbiz.fetchByDate(pageNum, pageSize);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("total", total);
		request.setAttribute("messageslist", messageslist);
		request.getRequestDispatcher("message-vistor.jsp").forward(request, response);
	}

}
